package com.charging.business.enums;


import com.charging.business.utils.ToolsUtil;

/**
 * 车种信息
 *
 * @author JerryLu
 * @date 2024/9/26 10:02
 * @return
 */
public class Vclass {
    /**
     * 普通车0x00
     */
    public static final int NORMAL = 0;

    /**
     * 公务车0x06
     */
    public static final int GW = 6;

    /**
     * 军警车0x08
     */
    public static final int AP = 8;

    /**
     * MTC紧急车0x0A
     */
    public static final int MTC_JJC = 10;

    /**
     * 免费0x0C
     */
    public static final int FREE = 12;

    /**
     * 车队0x0E
     */
    public static final int VQ = 14;

    /**
     * MTC绿通0x15
     */
    public static final int MTC_GREEN = 21;

    /**
     * MTC联合收割机0x16
     */
    public static final int MTC_COMHV = 22;

    /**
     * MTC抢险救灾0x17
     */
    public static final int MTC_RESCUE = 23;

    /**
     * J1专用集装箱0x18
     */
    public static final int J1 = 24;

    /**
     * MTC大件运输车0x19
     */
    public static final int MTC_LARGE = 25;

    /**
     * 应急救援车0x1A
     */
    public static final int ETC_EMER = 26;

    /**
     * 甩挂车0x1B
     */
    public static final int TRAILER = 27;

    /**
     * J2集装箱0x1C
     */
    public static final int J2 = 28;

    /**
     * 车种翻译
     */
    public static String trans(int vclass) {
        switch (vclass) {
            case NORMAL:
                return "普通车";
            case GW:
                return "公务";
            case AP:
                return "军警车";
            case MTC_JJC:
                return "紧急车";
            case FREE:
                return "免费";
            case VQ:
                return "车队";
            case MTC_GREEN:
                return "绿通";
            case MTC_COMHV:
                return "联合收割";
            case MTC_RESCUE:
                return "抢险救灾";
            case J1:
                return "J1集装箱";
            case MTC_LARGE:
                return "大件运输车";
            case ETC_EMER:
                return "应急救援";
            case TRAILER:
                return "挂车";
            case J2:
                return "J2集装箱";
        }
        return "未知车种";
    }

    /**
     * 车种校验
     *
     * @param vclass
     * @return
     */
    public static boolean tstVClass(int vclass) {
        return isNumIn(vclass, NORMAL, GW, AP, MTC_JJC, FREE, VQ, MTC_GREEN, MTC_COMHV, MTC_RESCUE, J1, MTC_LARGE, ETC_EMER, TRAILER, J2);
    }

    /**
     * 车种校验
     *
     * @param isEntry - true:入口; false:出口
     * @param bETC    - true:ETC; false:非ETC
     * @param vtype   - 车型
     * @param vclass  - 待校验车种
     * @return true - 校验通过 false - 校验失败
     */
    public static boolean tstVClass(boolean isEntry, boolean bETC, int vtype, int vclass) {
        // 统一检查
        if (!isNumIn(vclass, NORMAL, GW, AP, MTC_JJC, FREE, VQ, MTC_GREEN, MTC_COMHV, MTC_RESCUE, J1, MTC_LARGE, ETC_EMER, TRAILER, J2)) {
            return false;
        }

        // 为客车检查
        if ((vtype >= 1 && vtype <= 4) && isNumIn(vclass, MTC_GREEN, MTC_COMHV, J1, MTC_LARGE, TRAILER, J2)) {
            return false;
        }

        // 通行介质种类检查
        if (bETC) {// ETC
            if (isNumIn(vclass, MTC_GREEN, MTC_COMHV, MTC_JJC, MTC_RESCUE, MTC_LARGE)) {
                return false;
            }
        } else {// MTC
            if (isNumIn(vclass, ETC_EMER)) {
                return false;
            }
        }

        if (isEntry) {// 入口
            return !isNumIn(vclass, MTC_GREEN, MTC_COMHV);
        }
        return true;
    }

    /**
     * 测试是否为入口免费车种
     * 该车种沿途计费为0
     */
    public static boolean isEnFree(int vclass) {
        return isNumIn(vclass, AP, MTC_JJC, VQ, MTC_RESCUE, ETC_EMER);
    }

    public static String[] array() {
        String[] res = new String[]{"99:所有车种", "0:普通车", "8:军警车", "10:MTC紧急车", "23:MTC抢险救灾", "24:J1专用集装箱", "25:MTC大件运输车", "26:应急救援车", "27:甩挂车", "28:J2集装箱"};
        return res;
    }

    public static String[] array(boolean bMTC) {
        if (bMTC) {
            return new String[]{"0:普通车", "8:军警车", "10:紧急车", "21:绿通", "22:联合收割机", "23:抢险救灾", "24:J1专用集装箱", "25:大件运输车", "27:甩挂车", "28:J2集装箱"};
        }
        return new String[]{"0:普通车", "8:军警车", "24:J1专用集装箱", "26:应急救援车", "27:甩挂车", "28:J2集装箱"};
    }

    /**
     * 测试是否为牵引车
     */
    public static boolean isTrailer(int vclass) {
        return isNumIn(vclass, J1, J2, MTC_LARGE, TRAILER);
    }

    /**
     * 转换MTC车种
     * 校验并转换军警车标识
     *
     * @param vtype        - 车型
     * @param cpuVClass    - 用户卡内车种
     *                     >=0 ：为正常车种
     *                     =-1 ：车种未知
     * @param oubVClass    - OBU内车种
     *                     >=0 ：为正常车种
     *                     =-1 ：车种未知
     * @param etcVehicleId - OBU或用户卡内车牌
     * @return
     */
    public static int getEtcVCLASS(int vtype, int cpuVClass, int oubVClass, String etcVehicleId) {
        int vclass = 0;
        if (cpuVClass >= 0 && oubVClass >= 0) {
            if (vtype < 10) {
                vclass = cpuVClass;        // 客车以卡内为准
            } else {
                vclass = oubVClass;    // 货车以OBU内为准
            }
        } else if (cpuVClass >= 0) {
            vclass = cpuVClass;
        } else if (oubVClass >= 0) {
            vclass = oubVClass;
        }

        // 军警校验
        if (vclass == Vclass.AP) {// 为军警
            if (etcVehicleId.contains("警")) {// 为警车
                if (etcVehicleId.contains("晋")) {// 为本省警车
                    // 保持不变
                } else {// 为外省警车
                    // 转普通车
                    vclass = Vclass.NORMAL;
                }
            } else {// 非警车
                // 保持不变
            }
        } else if (etcVehicleId.contains("警") && etcVehicleId.contains("晋")) {// 本省警车强制为军警
            vclass = Vclass.AP;
        }
        return vclass;
    }

    /**
     * 转换MTC车种
     * 本省警车强制转军警
     * 外省警车强制转普通
     * 军车及武警车强制转军警
     *
     * @param p_vclass  - 原车种
     * @param vehicleId - 车牌
     * @return
     */
    public static int getMtcVCLASS(int p_vclass, String vehicleId) {
        int vclass = p_vclass;

        // 军警校验
        if (vclass == Vclass.AP) {// 为军警
            if (vehicleId.contains("警")) {// 为警车
                if (vehicleId.contains("晋")) {// 为本省警车
                    // 保持不变
                } else {// 为外省警车
                    // 转普通车
                    vclass = Vclass.NORMAL;
                }
            } else if (isArmyPlate(vehicleId)) {// 为军车
                // 保持不变
            } else {
                // 转普通车
                vclass = Vclass.NORMAL;
            }
        } else if (vehicleId.contains("警") && vehicleId.contains("晋")) {// 本省警车强制为军警
            vclass = Vclass.AP;
        } else if (isArmyPlate(vehicleId)) {// 为军车及武警车辆
            vclass = Vclass.AP;
        }
        return vclass;
    }

    /**
     * 武警车牌判断
     *
     * @param vehicleId
     * @return
     */
    public static boolean isWJPlate(String vehicleId) {
        vehicleId = vehicleId.toUpperCase();
        return vehicleId.startsWith("WJ");
    }

    /**
     * 军车、武警车车牌判断
     *
     * @param vehicleId
     * @return
     */
    public static boolean isArmyPlate(String vehicleId) {
        vehicleId = vehicleId.toUpperCase();
        if (isWJPlate(vehicleId)) {
            return true;
        }
        return vehicleId.matches("^[A-Z]{1}[A-Z0-9_]*");
    }

    /**
     * 判断是否为军队发行方
     *
     * @param issuerId - 发行机构编码
     * @return
     */
    public static boolean isArmyETC(String issuerId) {
        String armyHex = ToolsUtil.str2hex("军", "gb2312");
        // 为军队发行方
        if (issuerId.contains(armyHex)) {// 为军队发行方
            return true;
        } else {
            return issuerId.startsWith("05");
        }
    }

    /**
     * 检查给定数是否是系列数之一
     */
    private static boolean isNumIn(int number, int... numbers) {
        for (int num : numbers) {
            if (number == num) {
                return true;
            }
        }
        return false;
    }
}
